package com.cloudptt.wp.ui.address.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.mcptt.db.model.AddrInfo;

import java.util.List;

public class AddrGroupListAdapter extends BaseAdapter {

	private static final String THIS_FILE = "AddrListAdapter";
	private Context ctx = null;
	private List<AddrInfo> list = null;
	private boolean isForwardUi = false;
//	private AddrInfo mcIdInfo = null;

	public AddrGroupListAdapter(Context ctx, List<AddrInfo> list,boolean isForwardUi)
	{
		Log.i(THIS_FILE,"AddrGroupListAdapter list =  "+ list + " isForwardUi = " + isForwardUi);
		this.ctx = ctx;
		this.list = list;
		this.isForwardUi = isForwardUi;
	}

	public void refresh(List<AddrInfo> list){
		Log.i(THIS_FILE,"refresh list =  "+ list);
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(list != null){
			return list.size();
		}else{
			return 0;
		}

	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE,"getItem position =  "+ position);
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null)
		{
			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = inflater.inflate(R.layout.item_group, null);

			viewHolder = new ViewHolder();
			viewHolder.itemLy = (LinearLayout)convertView.findViewById(R.id.itemLy);
			viewHolder.userIcon       = (ImageView)convertView.findViewById(R.id.user_icon);
			viewHolder.userInfoLy =  (View)  convertView.findViewById(R.id.user_info);
			viewHolder.name    =  (TextView)  convertView.findViewById(R.id.user_name);

			viewHolder.defaultTv = (TextView)convertView.findViewById(R.id.defaultTv);
			viewHolder.curTv = (TextView)convertView.findViewById(R.id.curTv);
			viewHolder.videoBtn  =  (ImageView) convertView.findViewById(R.id.video_button);
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if(isForwardUi)
		{
			viewHolder.itemLy.setBackground(null);
			viewHolder.videoBtn.setVisibility(View.INVISIBLE);
		}

		final AddrInfo addrInfo = list.get(position);
		Log.i(THIS_FILE,"getView addrInfo =  "+ addrInfo);

		if(addrInfo.isGroupType())
		{
			if(addrInfo.isAffiliation()){
				viewHolder.defaultTv.setVisibility(View.VISIBLE);
			}else{
				viewHolder.defaultTv.setVisibility(View.GONE);
			}
			Log.i(THIS_FILE,"getView  addrInfo = " + addrInfo);
			viewHolder.curTv.setVisibility(View.GONE);
		}
		viewHolder.videoBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v)
			{
				v.setEnabled(false); // 立即禁用按钮
				// 500ms 后恢复可用
				v.postDelayed(() -> v.setEnabled(true), 1000);

				SysApplication app = ((SysApplication) ctx.getApplicationContext());
				Product product = app.getProduct();
				product.setCurGroupInfo(addrInfo);

				if(product.getGCallEventAdapter() != null){
					product.getGCallEventAdapter().onPtt(Const.MSG_TALK_ON, true,true,true,false);
				}
			}
		});
		String dscp = addrInfo.getDscp();
		viewHolder.name.setText(dscp);

		return convertView;
	}


	private class ViewHolder {
		public LinearLayout itemLy;
		public ImageView userIcon;
		public View userInfoLy;
		public TextView  name;
		public TextView defaultTv;
		public TextView curTv;
		public ImageView videoBtn;
	}
}
